﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace olkbutton4
{
    class OlkConfig
    {
        public RegistryKey baseRegistryKey = Registry.CurrentUser;
        public string registrySubKey = "SOFTWARE\\olkbutton4";
        private bool showError = true;

        public class OlkButton4Config
        {
            public String subjectStringConfidential = "[PGP]";
            public String subjectStringPrivate = "[PRIVATE]";
            public String subjectStringPersonal = "[PERSONAL]";
            public Outlook.OlSensitivity defaultSensitivity = Outlook.OlSensitivity.olNormal;
            public bool addSubjectConfidential = false;
            public bool addSubjectPrivate = false;
            public bool addSubjectPersonal = false;
            public bool showConfiButton = true;
            public bool showPrivateButton = true;
            public bool showPersonalButton = true;
            public bool showNoneButton = false;

            public OlkButton4Config()  // Constructor
            {}
        }

        public OlkButton4Config GetConfig()
        {
            OlkButton4Config myConfig = new OlkButton4Config();
            myConfig.addSubjectConfidential = ReadRegaddSubjectConfidential();
            myConfig.addSubjectPersonal = ReadRegaddSubjectPersonal();
            myConfig.addSubjectPrivate = ReadRegaddSubjectPrivate();
            myConfig.showConfiButton = ReadRegshowConfiButton();
            //myConfig.showNoneButton = = ReadRegshowNoneButton();   // for future use
            myConfig.showPersonalButton = ReadRegshowPersonalButton();
            myConfig.showPrivateButton = ReadRegshowPrivateButton();
            myConfig.subjectStringConfidential = ReadRegsubjectStringConfi();
            myConfig.subjectStringPersonal = ReadRegsubjectStringPersonal();
            myConfig.subjectStringPrivate = ReadRegsubjectStringPrivate();
            myConfig.defaultSensitivity = ReadRegdefaultSensitivity();
            return myConfig;
        }

        private Outlook.OlSensitivity ReadRegdefaultSensitivity()
        {
            String value = ReadReg("defaultSensitivity");
            Outlook.OlSensitivity myDefault = Outlook.OlSensitivity.olNormal;
            if (value != null)
            {
                switch (value.ToLower())
                {
                    case "confidential":
                        return Outlook.OlSensitivity.olConfidential;
                        break;
                    case "private":
                        return Outlook.OlSensitivity.olPrivate;
                        break;
                    case "personal":
                        return Outlook.OlSensitivity.olPersonal;
                        break;
                    default:
                        return myDefault;
                }
            }
            else
            {
                if (WriteReg("defaultSensitivity", "normal")) return ReadRegdefaultSensitivity();
            }
            return myDefault;
        }

        private string ReadRegsubjectStringPrivate()
        {
            String value = ReadReg("subjectStringPrivate");
            String myDefault = "[PRIVATE]";
            if (value != null)
            {
                return value;
            }
            else
            {
                if (WriteReg("subjectStringPrivate", myDefault)) return ReadRegsubjectStringPrivate();
            }
            return myDefault;
        }

        private string ReadRegsubjectStringPersonal()
        {
            String value = ReadReg("subjectStringPersonal");
            String myDefault = "[PERSONAL]";
            if (value != null)
            {
                return value;
            }
            else
            {
                if (WriteReg("subjectStringPersonal", myDefault)) return ReadRegsubjectStringPersonal();
            }
            return myDefault;
        }

        private string ReadRegsubjectStringConfi()
        {
            String value = ReadReg("subjectStringConfidential");
            String myDefault = "[PGP]";
            if (value != null)
            {
                return value;
            }
            else
            {
                if (WriteReg("subjectStringConfidential", myDefault)) return ReadRegsubjectStringConfi();
            }
            return myDefault;
        }

        private bool ReadRegshowPrivateButton()
        {
            String value = ReadReg("showPrivateButton");
            String myDefault = "1";
            if (value != null)
            {
                if (value.Equals("1")) { return true; } else { return false; }
            }
            else
            {
                if (WriteReg("showPrivateButton", myDefault)) return ReadRegshowPrivateButton();
            }
            if (myDefault.Equals("1")) { return true; } else { return false; };
        }

        private bool ReadRegshowPersonalButton()
        {
            String value = ReadReg("showPersonalButton");
            String myDefault = "1";
            if (value != null)
            {
                if (value.Equals("1")) { return true; } else { return false; }
            }
            else
            {
                if (WriteReg("showPersonalButton", myDefault)) return ReadRegshowPersonalButton();
            }
            if (myDefault.Equals("1")) { return true; } else { return false; };
        }

        private bool ReadRegshowConfiButton()
        {
            String value = ReadReg("showConfidentialButton");
            String myDefault = "1";
            if (value != null)
            {
                if (value.Equals("1")) { return true; } else { return false; }
            }
            else
            {
                if (WriteReg("showConfidentialButton", myDefault)) return ReadRegshowConfiButton();
            }
            if (myDefault.Equals("1")) { return true; } else { return false; };
        }

        private bool ReadRegaddSubjectPrivate()
        {
            String value = ReadReg("addSubjectPrivate");
            String myDefault = "0";
            if (value != null)
            {
                if (value.Equals("1")) { return true; } else { return false; }
            }
            else
            {
                if (WriteReg("addSubjectPrivate", myDefault)) return ReadRegaddSubjectPrivate();
            }
            if (myDefault.Equals("1")) { return true; } else { return false; };
        }

        private bool ReadRegaddSubjectPersonal()
        {
            String value = ReadReg("addSubjectPersonal");
            String myDefault = "0";
            if (value != null)
            {
                if (value.Equals("1")) { return true; } else { return false; }
            }
            else
            {
                if (WriteReg("addSubjectPersonal", myDefault)) return ReadRegaddSubjectPersonal();
            }
            if (myDefault.Equals("1")) { return true; } else { return false; };
        }

        private bool ReadRegaddSubjectConfidential()
        {
            String value = ReadReg("addSubjectConfidential");
            String myDefault = "0";
            if (value != null)
            {
                if (value.Equals("1")) { return true; } else { return false; }
            }
            else
            {
                if (WriteReg("addSubjectConfidential", myDefault)) return ReadRegaddSubjectConfidential();
            }
            if (myDefault.Equals("1")) { return true; } else { return false; };
        }

        public string ReadReg(string KeyName)
        {
            RegistryKey rk = baseRegistryKey;
            RegistryKey sk1 = rk.OpenSubKey(registrySubKey);
            // If the RegistrySubKey doesn't exist -> (null)
            if (sk1 == null)
            {
                return null;
            }
            else
            {
                try
                {
                    // If the RegistryKey exists I get its value or null is returned.
                    return (string)sk1.GetValue(KeyName.ToUpper());
                }
                catch (Exception e)
                {
                    ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
                    return null;
                }
            }
        }

        public bool WriteReg(string KeyName, object Value)
        {
            try
            {
                // Setting
                RegistryKey rk = baseRegistryKey;
                // I have to use CreateSubKey 
                // (create or open it if already exits), 
                // 'cause OpenSubKey open a subKey as read-only
                RegistryKey sk1 = rk.CreateSubKey(registrySubKey);
                // Save the value
                sk1.SetValue(KeyName.ToUpper(), Value);

                return true;
            }
            catch (Exception e)
            {
                ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
                return false;
            }
        }
        
        private void ShowErrorMessage(Exception e, string Title)
        {
            if (showError == true)
                MessageBox.Show(e.Message,
                        Title
                        , MessageBoxButtons.OK
                        , MessageBoxIcon.Error);
        }
    }
}
